from celery.task import task
from .utils import MysqlFind
import re
from email.mime.text import MIMEText
from email.utils import formataddr
import smtplib

# 定义参数
my_mail = '347155713@qq.com'
# 邮箱授权码
my_pass = 'oowgpztkfebkbjed'

# 发送邮箱问候
@task
def timeing_email():
	sql = 'select username,email from user'
	result = MysqlFind(sql).sql_select()
	for item in result:

		# 声明邮件对象
		msg = MIMEText("你好啊", 'plain', 'utf-8')

		# 设置发送方对象
		msg['From'] = formataddr(['在线教育平台', my_mail])

		# 设置收件方对象
		msg['To'] = formataddr(['尊敬的客户', item.get("email")])

		# 设置标题
		msg['Subject'] = '祝你天天开心'

		# 设置smtp服务器
		server = smtplib.SMTP_SSL("smtp.qq.com", 465)

		# 登录邮箱
		server.login(my_mail, my_pass)

		# 发送邮件
		print(item.get("username"),item.get("email"))
		server.sendmail(my_mail, [item.get("email")], msg.as_string())

		# 关闭smtp链接
		server.quit()
